﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.DataAccessLayer;
using System.Data;
using System.Windows.Forms;
using CXMLStudioIDE.Common;

namespace CXMLStudioIDE.BusinessLayer
{
    public class BusinessEntityBuilder
    {
        #region Constructors

        public BusinessEntityBuilder()
        {
        }

        #endregion

        #region Methods

        private string[] UpdateBusinessEntityCodeView(int i_EntityTypeVersionID)
        {
            DataSet DsBusinessEntityCode = null;
            CXMLStudioDAL dal = new CXMLStudioDAL();
            DsBusinessEntityCode = dal.GetVersionedBusinessEntityAndCode(i_EntityTypeVersionID);
            string[] sz_CsEntityCode = new string[DsBusinessEntityCode.Tables[0].Rows.Count];

            for (int i = 0; i < DsBusinessEntityCode.Tables[0].Rows.Count; i++)
            {
                sz_CsEntityCode[i] = DsBusinessEntityCode.Tables[0].Rows[i].ItemArray[3].ToString();
            }
            return sz_CsEntityCode;

        }

        public Type GetBusinessEntityType(BusinessEntity be)
        {
            string[] sz_BusinessEntityCode = UpdateBusinessEntityCodeView(be.EntityTypeVersions.EntityTypeVersionsID);
            Type t_BusinessEntityType = BuildAndLoadBusinessEntity(sz_BusinessEntityCode, be);
            return t_BusinessEntityType;
        }

        private Type BuildAndLoadBusinessEntity(string[] sz_BusinessEntityCode, BusinessEntity be)
        {
            int BusinessEntityID = be.EntityType.EntityTypeID;
            DataSet dsToCompile = MainFormBL.BuildDataSet(be);

            string[] Code = new string[dsToCompile.Tables[0].Rows.Count];
            for (int i = 0; i < dsToCompile.Tables[0].Rows.Count; i++)
            {
                Code[i] = dsToCompile.Tables[0].Rows[i].ItemArray[0].ToString();
            }

            Type T = null;
            System.CodeDom.Compiler.CompilerErrorCollection compileErrors = null;
            string errors = "";
            try
            {
                /* Compile Code and add to Assembly */
                /* Serialize to an XSD */
                Compiler c = new Compiler();
                System.CodeDom.Compiler.CompilerResults compileResults = c.CompileClass(Code, be.Name);
                compileErrors = compileResults.Errors;

                if (compileErrors.Count > 0)
                {
                    for (int i = 0; i < compileErrors.Count; i++)
                    {
                        errors += compileErrors[i].ErrorText + "\n";
                    }
                }
                System.Reflection.Assembly Assem = compileResults.CompiledAssembly;
                string AssemblyName = "Enterprise." + be.Name;
                T = Assem.GetType(AssemblyName);
            }
            catch (Exception er)
            {
                MessageBox.Show("Error:" + er.Message + "\nCompileErrors:\n" + errors, "CXMLStudioIDE - Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return T;
        }

        #endregion Methods
    }
}
